//
//  RegisterScreen.m
//  Gekko
//
//  Created by jose on 30/06/13.
//  Copyright (c) 2013 Jose Luis Lopez. All rights reserved.
//

#import "RegisterScreen.h"

@interface RegisterScreen ()

@end

@implementation RegisterScreen

@synthesize scrollView, labelSlogan, labelCompany, labelEmail, labelName, labelPassword, labelRepeatPassword, inputCompany, inputEmail, inputName, inputPassword, inputRememberPassword, buttonRegister, buttonToLogin, labelPrivacy;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [labelSlogan setText:NSLocalizedString(@"SLOGAN", nil)];
    [labelSlogan setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [labelCompany setText:NSLocalizedString(@"R COMPANY", nil)];
    [labelCompany setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [labelName setText:NSLocalizedString(@"R NAME", nil)];
    [labelName setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [labelEmail setText:NSLocalizedString(@"R EMAIL", nil)];
    [labelEmail setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [labelPassword setText:NSLocalizedString(@"R PASSWORD", nil)];
    [labelPassword setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [labelRepeatPassword setText:NSLocalizedString(@"R REPEAT PASSWORD", nil)];
    [labelRepeatPassword setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [inputCompany setFont:[UIFont fontWithName:kFontLight size:16]];
    [inputCompany setPlaceholder:NSLocalizedString(@"R PLA COMPANY", nil)];
    
    [inputName setFont:[UIFont fontWithName:kFontLight size:16]];
    [inputName setPlaceholder:NSLocalizedString(@"R PLA NAME", nil)];
    
    [inputEmail setFont:[UIFont fontWithName:kFontLight size:16]];
    [inputEmail setPlaceholder:NSLocalizedString(@"R PLA EMAIL", nil)];
    
    [inputPassword setFont:[UIFont fontWithName:kFontLight size:16]];
    
    [inputRememberPassword setFont:[UIFont fontWithName:kFontLight size:16]];
    
    [buttonRegister setTitle:NSLocalizedString(@"R REGISTER", nil) forState:UIControlStateNormal];
    [buttonRegister.titleLabel setFont:[UIFont fontWithName:kFontBold size:16]];

    [buttonToLogin setTitle:NSLocalizedString(@"R BACK", nil) forState:UIControlStateNormal];
    [buttonToLogin.titleLabel setFont:[UIFont fontWithName:kFontBold size:16]];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
    [self.view addGestureRecognizer:tap];
    
    [scrollView setContentSize:CGSizeMake(320, labelPrivacy.frame.origin.y + labelPrivacy.frame.size.height + 20)];
    
    [labelPrivacy setText:NSLocalizedString(@"PRIVACY", nil)];
    [labelPrivacy setFont:[UIFont fontWithName:kFontBold size:12]];
    [labelPrivacy sizeToFit];
    
    UIButton *buttonLearn = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonLearn setFrame:labelPrivacy.frame];
    [buttonLearn addTarget:self action:@selector(privacyPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonLearn];
}

-(void)tapped:(UITapGestureRecognizer *)gesture {
    
    [inputEmail resignFirstResponder];
    [inputCompany resignFirstResponder];
    [inputName resignFirstResponder];
    [inputPassword resignFirstResponder];
    [inputRememberPassword resignFirstResponder];
}

- (IBAction)registerPressed:(UIButton *)sender {
    
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"OK", nil), nil];
    
    if ([inputCompany.text length] < 1 || [inputName.text length] < 1 || [inputEmail.text length] < 1 ||
        [inputPassword.text length] < 1) {
        [alert setMessage:NSLocalizedString(@"R ALL FIELDS", nil)];
    }
    if (![inputPassword.text isEqualToString:inputRememberPassword.text]) {
        [alert setMessage:NSLocalizedString(@"R PASSWORD MISSMATCH", nil)];
    }
    
    
    if (YES) {
        [alert setMessage:NSLocalizedString(@"R SUCCESS", nil)];
        [alert setDelegate:self];
    }
    
    [alert show];
}

- (IBAction)backToLoginPressed:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    
    NSInteger distanceToMove = 0;
    
    if (textField == inputName)
        distanceToMove = - 24;
    else if (textField == inputEmail)
        distanceToMove = - 24 - 44;
    else if (textField == inputPassword) 
        distanceToMove = - 24 - 88;
    else if (textField == inputRememberPassword)
        distanceToMove = - 24 - 88 - 44;
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y + distanceToMove);
    
    [UIView commitAnimations];
}


-(void)textFieldDidEndEditing:(UITextField *)textField {
    
    NSInteger distanceToMove = 0;
    
    if (textField == inputName)
        distanceToMove = 24;
    else if (textField == inputEmail)
        distanceToMove = 24 + 44;
    else if (textField == inputPassword) 
        distanceToMove = 24 + 88;
    else if (textField == inputRememberPassword)
        distanceToMove = 24 + 88 + 44;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y + distanceToMove);
    
    [UIView commitAnimations];
}

-(void)privacyPressed:(UIButton *)sender {
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:kURLPrivacy]];
}
- (void)viewDidUnload {
    [self setLabelSlogan:nil];
    [super viewDidUnload];
}
@end
